#include <stdio.h>
#include "sql.h"

void print_menu()
{
    printf("\n====== 学生成绩管理系统 ======\n");
    printf("1. 创建数据表\n");
    printf("2. 插入数据\n");
    printf("3. 删除数据\n");
    printf("4. 更新成绩\n");
    printf("5. 查询学生\n");
    printf("6. 显示全部数据\n");
    printf("0. 退出\n");
    printf("请输入操作编号: ");
}

int main()
{
    const char* dbName = "students.db";
    const char* tableName = "student";
    sqlite3* db = mysql_open(dbName);

    int choice;
    dataStruct stu;
    char name[64];

    while (1)
    {
        // system("clear");
        print_menu();
        scanf("%d", &choice);
        getchar();  // 吸收换行符

        switch (choice)
        {
        case 1:
            mysql_create(db, tableName);
            break;
        case 2:
            printf("请输入id name sex score（空格分隔）:\n");
            scanf("%d %s %s %d", &stu.id, stu.name, stu.sex, &stu.score);
            mysql_insert(db, tableName, stu);
            break;
        case 3:
            printf("请输入要删除的学生姓名:\n");
            scanf("%s", name);
            mysql_delete_name(db, tableName, name);
            break;
        case 4:
            printf("请输入要修改成绩的学生姓名和新成绩:\n");
            scanf("%s %d", name, &stu.score);
            mysql_update(db, tableName, name, stu.score);
            break;
        case 5:
        {
            printf("请输入要查询的学生姓名:\n");
            scanf("%s", name);
            dataStruct result = mysql_find(db, tableName, name);
            if (result.id != -1)
            {
                printf("查找结果：ID=%d, 姓名=%s, 性别=%s, 成绩=%d\n",
                    result.id, result.name, result.sex, result.score);
            }
            break;
        }
        case 6:
            mysql_show(db, tableName);
            break;
        case 0:
            mysql_close(db);
            return 0;
        default:
            printf("无效的选项，请重新输入。\n");
            break;
        }
    }

    return 0;
}
